import { FC } from 'react';

// css modules 用于做css的作用域
// 好处在于不会去影响其他页面了
// 坏处在于less不能做嵌套了
import styles from './index.less';

// 直接可以使用antd，并且可以直接按需加载
import { Button, Tree } from 'antd';
import type { DataNode, TreeProps } from 'antd/es/tree';

// umi里面可以解构history对象用于编程式导航
import { Link, useHistory, history } from 'umi';

console.log('link加载了');

const treeData: DataNode[] = [
  {
    title: 'parent 1',
    key: '0-0',
    children: [
      {
        title: 'parent 1-0',
        key: '0-0-0',
        disabled: true,
        children: [
          {
            title: 'leaf',
            key: '0-0-0-0',
            disableCheckbox: true,
          },
          {
            title: 'leaf',
            key: '0-0-0-1',
          },
        ],
      },
      {
        title: 'parent 1-1',
        key: '0-0-1',
        children: [
          {
            title: <span style={{ color: '#1890ff' }}>sss</span>,
            key: '0-0-1-0',
          },
        ],
      },
    ],
  },
];

const Index: FC = () => {
  // const history = useHistory();

  // const fn = () => {
  //   history.push('/about');
  // };

  const fn = () => {
    history.push('/about');
  };

  const onSelect: TreeProps['onSelect'] = (selectedKeys, info) => {
    console.log('selected', selectedKeys, info);
  };

  const onCheck: TreeProps['onCheck'] = (checkedKeys, info) => {
    console.log('onCheck', checkedKeys, info);
  };

  return (
    <>
      <h2 className={styles.box}>页面跳转</h2>
      {/* 声明式 */}
      {/* <Link to="/about">about</Link> */}
      <Link to={{ pathname: '/about' }}>about</Link>

      {/* 命令式 */}
      <Button type="primary" onClick={fn}>
        btn
      </Button>

      <Tree
        checkable
        defaultExpandedKeys={['0-0-0', '0-0-1']}
        defaultSelectedKeys={['0-0-0', '0-0-1']}
        defaultCheckedKeys={['0-0-0', '0-0-1']}
        onSelect={onSelect}
        onCheck={onCheck}
        treeData={treeData}
      />
    </>
  );
};

export default Index;
